package com.hf.eagle.web.struts;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.AdvertisementConfig;
import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.OrderInfo;
import com.hf.eagle.domain.OrderProductDetail;
import com.hf.eagle.web.base.BaseAction;

/**
 * 在线订购
 * 
 * @author xiaokeheng
 */
public class OnlineOrderAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.order(mapping, form, request, response);
	}

	/**
	 * 显示在线订购
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward order(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		NewsInfo promotion = new NewsInfo();
		promotion.setMod_id(2902l);
		promotion.setIs_del(0);
		promotion.getMap().put("efficient_promotion", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		List<NewsInfo> promotionList = super.getFacade().getNewsInfoService().getNewsInfoList(promotion);
		request.setAttribute("promotionList", promotionList);
		request.setAttribute("mod_id", mod_id);
		return mapping.findForward("input");
	}

	@SuppressWarnings("finally")
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String news_id = (String) dynaBean.get("news_id");
		String name = (String) dynaBean.get("name");
		String sex = (String) dynaBean.get("sex");
		String mobile = (String) dynaBean.get("mobile");
		String QQ = (String) dynaBean.get("QQ");
		String address = (String) dynaBean.get("address");
		String memo = (String) dynaBean.get("memo");
		String total_price = (String) dynaBean.get("total_price");
		String verificationCode = (String) dynaBean.get("verificationCode");
		String[] product_ids = request.getParameterValues("product_id");
		String[] nums = request.getParameterValues("num");

		String flag = "1";

		HttpSession session = request.getSession();
		if (!verificationCode.equals((String) session.getAttribute("verificationCode"))) {
			flag = "2";
			super.renderJson(response, flag);
			return null;
		} else {
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setMod_id(Long.valueOf(mod_id));
			orderInfo.setName(name);
			orderInfo.setSex(Integer.valueOf(sex));
			orderInfo.setMobile(mobile);
			orderInfo.setQq(QQ);
			orderInfo.setAddress(address);
			orderInfo.setMemo(memo);
			orderInfo.setState(0);// 未处理
			orderInfo.setTotal_price(Double.valueOf(total_price));

			if ("2902".equals(mod_id)) {// 促销活动
				orderInfo.setNews_id(Long.valueOf(news_id));
			} else { // 促销产品
				orderInfo.setTitle("产品在线订购");
				List<OrderProductDetail> list = new ArrayList<OrderProductDetail>();
				if (null != product_ids && product_ids.length > 1) {
					for (int i = 1; i < product_ids.length; i++) {
						OrderProductDetail orderProductDetail = new OrderProductDetail();
						orderProductDetail.setProduct_id(Long.valueOf(product_ids[i]));
						orderProductDetail.setNum(Integer.valueOf(nums[i]));
						list.add(orderProductDetail);
					}
				}
				orderInfo.setProductDetailList(list);
			}

			try {
				super.getFacade().getOrderInfoService().createOrderInfo(orderInfo);
			} catch (Exception e) {
				flag = "-1";
				e.printStackTrace();
			} finally {
				super.renderJson(response, flag);
				return null;
			}
		}
	}
}